package com.topfinance.cnaps2.web.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.topfinance.payment.ebo.TCfgProtocolEbo;
import com.topfinance.payment.service.ITCfgProtocolMgr;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class CfgProtocolAction extends BaseSupport implements ModelDriven<TCfgProtocolEbo> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3752267860849470550L;

	private ITCfgProtocolMgr protocolManager = null;

	private TCfgProtocolEbo prtclEbo = new TCfgProtocolEbo();

	public TCfgProtocolEbo getModel() {
		return prtclEbo;
	}

	public void setProtocolManager(ITCfgProtocolMgr protocolManager) {
		this.protocolManager = protocolManager;
	}

	public CfgProtocolAction() {
		super();
	}

	public String execute() {
		return list();
	}

	public String list() {
		log.debug("CfgTransAction-list begin");
		try {
			List<TCfgProtocolEbo> l = protocolManager.getAllPrtcl();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgTransAction-list end");
		return SUCCESS;
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(prtclEbo));
			protocolManager.addProtocol(prtclEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + prtclEbo.getOid();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(prtclEbo));
			protocolManager.editProtocol(prtclEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			String delId = request.getParameter("id");
			protocolManager.deleteProtocol(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	
	
	public TCfgProtocolEbo getProtocolEbo() {
		return prtclEbo;
	}

	public void setProtocolEbo(TCfgProtocolEbo transEbo) {
		this.prtclEbo = transEbo;
	}

	public String query() {
		if (request.getParameter("id") == null) {
			rtnResultMap = new HashMap();
			String[] args = new String[1];
			args[0] = "";// you can add extra message here
			rtnResultMap.put("info", getText("expect.valid.pk.data", args));
			return SUCCESS;
		}
		try {
			String uid = request.getParameter("id");
			TCfgProtocolEbo trans = protocolManager.getProtocol(new Integer(uid));
			List l = new ArrayList<TCfgProtocolEbo>();
			l.add(trans);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, 1);
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	
	
	public String test() {
		log.debug("SysFuncAction-test begin");
		try {
			TCfgProtocolEbo pEbo = new TCfgProtocolEbo();
			pEbo.setName("PROTOCOL_SWIFT");
//			pEbo.setPluginName("SWIFT");
//			pEbo.setUpSendAckToPP("F");
//			pEbo.setDownRecievePPAck("F");			
//			protocolManager.addProtocol(pEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("info", getText("create.success", "Test Success!"));
			return SUCCESS;
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysFuncAction-list end");
		return SUCCESS;
	}

}
